﻿using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq.Expressions;

namespace TCC.CMFlow.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class Usuario : IdentityUser
    {
        [Display(Name = "Código")]
        public int Codigo { get; set; }

        [Required]
        [Display(Name = "Nome")]
        public virtual string Nome { get { return base.UserName; } }

        [Required]
        [Display(Name = "Perfil")]
        public int PerfilAcessoID { get; set; }

        [Display(Name = "E-mail")]
        public string Email { get; set; }  
        
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Início vigência")]
        public DateTime? InicioVigencia { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Fim vigência")]
        public DateTime? FimVigencia { get; set; }

        [Display(Name = "Observações")]
        public string Observacoes { get; set; }

        [Display(Name = "Status")]
        public string Status { get { return this.IsAtivo ? "Ativo" : "Inativo"; } }


        [Display(Name = "Perfil")]
        public virtual PerfilAcesso PerfilAcesso { get; set; }

        public virtual ICollection<GrupoAprovacao> GruposAprovacao { get; set; }

        public ICollection<CategoriaGrupo> CategoriasGrupo { get; set; }

        public ICollection<WorkflowAprovacao> WorkflowAprovacoes { get; set; }

        public ICollection<LogSistema> LogSistema { get; set; }

        public virtual bool IsAtivo
        {
            get
            {
                return IsAtivoExpression.Compile()(this);
            }
        }

        public static Expression<Func<Usuario, bool>> IsAtivoExpression
        {
            get
            {
                return t => t.InicioVigencia.HasValue && t.InicioVigencia.Value <= DateTime.Today && (!t.FimVigencia.HasValue || t.FimVigencia.Value >= DateTime.Today);
            }
        }

        public static Expression<Func<Usuario, bool>> IsInativoExpression
        {
            get
            {
                return t => !(t.InicioVigencia.HasValue && t.InicioVigencia.Value <= DateTime.Today && (!t.FimVigencia.HasValue || t.FimVigencia.Value >= DateTime.Today));
            }
        }
    }
    
}